/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
  January 7th, 2021

	DESCRIPTION: Creates Figures A.2 Of Appendix

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/FigA2`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${intermediate_data_emp}/dataset_AKM_estimation.dta", clear

/****************************************************************************************
* Run Figures
****************************************************************************************/

  *Appendix Figure A.2 (a) (Sorting of Regular and Temp Agency Workers: Estimated Worker Effects Plotted Against Firm Effects)
	*Binscatter Y-Axis: Worker FE (Regular); X-Axis: Regular Firm FE
	binscatter worker_fe4 firm_fe4 [fw=obs] if firm_fe4 != 0 & firm_fe4 != . & worker_fe4 != 0 & worker_fe4 != . & uf_fe4 != 0 & uf_fe4 != . & temp_worker == 0, ytitle("Worker FE", size(medium)) xtitle("Firm FE for Regular Workers", size(medium))  xsize(16) ysize(9)
	graph export "${Results}/Figure_A2a_appendix_RegWorkers.pdf", replace

	qui reg worker_fe4 firm_fe4 [fw=obs] if firm_fe4 != 0 & firm_fe4 != . & worker_fe4 != 0 & worker_fe4 != . & uf_fe4 != 0 & uf_fe4 != . & temp_worker == 0
	local beta_firm_fe4_a1a_reg = _b[firm_fe4]
	local se_firm_fe4_a1a_reg = _se[firm_fe4]

  *Appendix Figure A.2 (b) (Sorting of Regular and Temp Agency Workers: Estimated Worker Effects Plotted Against Firm Effects)
	*Binscatter Y-Axis: Worker FE (Temp); X-Axis: Regular Firm FE
	binscatter worker_fe4 firm_fe4 [fw=obs] if firm_fe4 != 0 & firm_fe4 != . & worker_fe4 != 0 & worker_fe4 != . & uf_fe4 != 0 & uf_fe4 != . & temp_worker > 0 & temp_worker !=., ytitle("Worker FE", size(medium)) xtitle("Firm FE for Regular Workers", size(medium))  xsize(16) ysize(9)
	graph export "${Results}/Figure_A2b_appendix_TempWorkers.pdf", replace

	qui reg worker_fe4 firm_fe4 [fw=obs] if firm_fe4 != 0 & firm_fe4 != . & worker_fe4 != 0 & worker_fe4 != . & uf_fe4 != 0 & uf_fe4 != . & temp_worker > 0 & temp_worker !=.
	local beta_firm_fe4_temp = _b[firm_fe4]
	local se_firm_fe4_temp = _se[firm_fe4]

  *Keep register of slopes and SE
	matrix beta= [(`beta_firm_fe4_a1a_reg',`beta_firm_fe4_temp')]'
	matrix s_e= [(`se_firm_fe4_a1a_reg',`se_firm_fe4_temp')]'

	cd "${Results}"

	putexcel set "Slope_and_SE_Figure_A2a_appendix", sheet("Slope_SE") modify
	putexcel A1=("Regression") B1=("Beta") C1=("SE")
	putexcel A2=("firm_fe4_a2a_reg") A3=("firm_fe4_a2b_temp")
	putexcel B2=matrix (beta) C2=matrix(s_e)

log close
